<?php

/**
 *
 * @author lovebing <lovebing365@126.com>
 * @property int $id 
 * @property int $uid 
 * @property string $pubun 
 * @property string $wun 
 * @property string $wpwd 
 * @property string $rtime 
 * @property int $isval 
 * @property string $http 
 * @property int $httptime 
 * @property string $token 
 * @property string $uuid 
 * @property string $cusid 
 * @property string $cussec 
 * @property string $headpic 
 * @property int $reval 
 * @property int $custom_domain_enable_flag
 * @property string $access_token 
 * @property string $access_token_expiry_time 
 */
class WechatSetting extends MainActiveRecord {

    /**
     * 
     * @return string
     */
    public function tableName() {
        return 'pubs';
    }

    /**
     * 
     * @param string $className
     * @return WechatSetting
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 
     * @param int $id
     * @return WechatSetting
     */
    public static function findById($id) {
        return self::model()->find('id=:id', array(':id' => $id));
    }

    /**
     * 
     * @return string
     */
    public function getAppId() {
        return $this->cusid;
    }

    /**
     * 
     * @return string
     */
    public function getSecretKey() {
        return $this->cussec;
    }

    /**
     * 
     * @return string
     */
    public function getToken() {
        return $this->token;
    }

    /**
     * 是否启用独立的网页授权域名
     * @return boolean
     */
    public function isCustomDomainEnable() {
        return $this->custom_domain_enable_flag == 1;
    }

    /**
     * 获取网页授权域名
     * @return string
     */
    public function getAuthDomain() {
        $general = 'w.xiaowangtong.com.cn';

        $special = $this->id . '.m.xiaowangtong.com.cn';

        return $this->isCustomDomainEnable() ? $special : $general;
    }

    /**
     * mobile域名
     * @return type
     */
    public function getMobileDomain() {
        $general = 'm.xiaowangtong.com.cn';
        $special = $this->id . '.' . $general;
        return $this->isCustomDomainEnable() ? $special : $general;
    }

    /**
     * 支付域名
     * @return type
     */
    public function getPaymentDomain() {
        $general = 'pay.xiaowangtong.com.cn';
        $special = $this->id . '.' . $general;
        return $this->isCustomDomainEnable() ? $special : $general;
    }

    /**
     * 
     * @param type $wid
     * @param type $access_token
     * @param type $appId
     * @return type
     * @throws RuntimeException
     */
    public static function getJsApiTicket($wid, $access_token, &$appId = '') {
        $model = self::findById($wid);

        if (empty($model)) {
            throw new RuntimeException('not found');
        }

        $appId = $model->getAppId();
        if ($model->jsapi_ticket_expiry_time == "0000-00-00 00:00:00" || strtotime($model->jsapi_ticket_expiry_time) < time()) {

            $jsApiTicketInfo = Wechat::getJsApiTicket($access_token);

            $model->jsapi_ticket = $jsApiTicketInfo['jsapi_ticket'];
            $model->jsapi_ticket_expiry_time = date('Y-m-d H:i:s', strtotime('+' . $jsApiTicketInfo['expires_in'] . ' seconds'));
            $model->save();
        }

        return $model->jsapi_ticket;
    }

    /**
     * 
     * @param type $wid
     * @param type $appId
     * @return type
     * @throws RuntimeException
     */
    public static function getAccessToken($wid, &$appId = '') {
        $model = self::findById($wid);
        if (empty($model)) {
            throw new RuntimeException('wid ' . $wid . 'not found');
        }
        $appId = $model->getAppId();
        $accessTokenInfo = "";
        $nGetCount = 3; //获取次数
        if (strtotime($model->access_token_expiry_time) < time()) {
            while ($nGetCount) {
                try {
                    $accessTokenInfo = Wechat::getAccessToken($model->getAppId(), $model->getSecretKey());
                    $nGetCount = 0;
                } catch (Exception $ex) {
                    $nGetCount--;
                    if (!$nGetCount) {
                        throw $ex;
                    }
                }
            }

            return $accessTokenInfo['access_token'];
        }
        return $model->access_token;
    }

}
